package gcontent.utils;

import gcontent.entities.content.error.ContentEx;

import java.util.HashMap;
import java.util.Iterator;

public class Utils {

	public static String getObjectName(Object obj)
	{
		Class<?> c;
		String cls_name;
		
		c = obj.getClass();
		
		cls_name = c.getName();
		return cls_name.substring(cls_name.lastIndexOf(".") + 1 , cls_name.length()).toLowerCase();
	}
	
	/* get a Content class from url protocol */
	public static Class<Object> getClassbyName(HashMap<String, String> map_rsrc, String classname) throws ContentEx
	{
		Iterator<String> map_key;

		map_key = map_rsrc.keySet().iterator();

		try { 
			while(map_key.hasNext()) {
				String key_name, value;

				key_name = map_key.next();	
				value = map_rsrc.get(key_name);
				if(value.toLowerCase().endsWith("." + classname.toLowerCase())) {
					return (Class<Object>)(Utils.class.getClassLoader().loadClass(value));	
				}
			}
		} catch(Exception ex) {
			ex.printStackTrace();
		}

		return null;
	}
}
